package com.franklin.ideaplugin.api.utils.spring;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.franklin.ideaplugin.api.utils.PatternUtil;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.module.Module;
import com.intellij.psi.PsiClass;
import com.intellij.spring.boot.application.SpringBootApplicationService;
import com.intellij.spring.boot.model.SpringBootConfigValueSearcher;
import com.intellij.spring.profiles.SpringProfileTarget;
import com.intellij.spring.profiles.SpringProfilesFactory;
import org.apache.groovy.util.Maps;

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author Ye Junhui
 * @since 2023/1/30 0030
 */
public class MySpringPropertyUtils {

    private MySpringPropertyUtils() {
    }

    /**
     * spring配置文件 EL表达式填值 ${key}
     */
    private final static Pattern EL_PATTERN = Pattern.compile(
            "\\$\\{([^\\}]+)\\}"
    );

    /**
     * 获取spring配置值
     *
     * @param module
     * @param propertyKey
     * @return
     */
    public static String getPropertyValue(Module module, String propertyKey) {
        if (Objects.isNull(module)) {
            return "";
        }
        SpringBootApplicationService springBootApplicationService = ServiceManager.getService(SpringBootApplicationService.class);
        List<PsiClass> springApplications = springBootApplicationService.getSpringApplications(module);
        if (CollectionUtil.isEmpty(springApplications)) {
            return "";
        }
        //启用配置文件
        List<SpringProfileTarget> springProfileTargetList = SpringProfilesFactory.getInstance().findProfileTargets(module, false);
        Set<String> activeProfileSet = springProfileTargetList.stream()
                .map(SpringProfileTarget::getName)
                .collect(Collectors.toSet());

        SpringBootConfigValueSearcher springBootConfigValueSearcher = SpringBootConfigValueSearcher.Companion.productionForProfiles(module, propertyKey, activeProfileSet);
        String propertyStringValue = springBootConfigValueSearcher.findValueText();
        String value = StrUtil.blankToDefault(propertyStringValue, "");
        return value;
    }

    /**
     * 填充属性
     * @param propertyKey
     * @param propertyValue
     * @param target
     * @return
     */
    public static String fillProperty(String propertyKey, String propertyValue,String target){
        Map<String,String> map = Maps.of(propertyKey,propertyValue);
        return fillProperties(map,target);
    }

    /**
     * 填值
     * @param propertyMap
     * @param target
     * @return
     */
    public static String fillProperties(Map<String,String> propertyMap,String target){
        return PatternUtil.fillValues(propertyMap,target,EL_PATTERN);
    }

    /**
     * 查找属性
     * @param target
     * @return
     */
    public static List<String> findProperties(String target){
        return PatternUtil.findKeys(target,EL_PATTERN);
    }
}
